package com.hlkj.tool.form;

import com.opensymphony.xwork2.ActionContext;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

@SuppressWarnings("unchecked")
public class FormRead {
	Map<String, Object> frommap;
	Map<String, ArrayList<Object>> caseMap;
	Object[] _objects;

	public FormRead() {
		this.frommap = ActionContext.getContext().getParameters();
		this.caseMap = new Hashtable();
		for (String key : this.frommap.keySet()) {
			ArrayList objs = new ArrayList();
			Object param = this.frommap.get(key);
			if (param == null) {
				continue;
			}
			if (param.getClass().isArray()) {
				Object[] objArray = (Object[]) this.frommap.get(key);
				for (Object o : objArray)
					objs.add(o);
			} else {
				objs.add(param);
			}

			String caseKey = key.toLowerCase().replace("[", "")
					.replace("]", "");

			if (this.caseMap.containsKey(caseKey))
				((ArrayList) this.caseMap.get(caseKey)).addAll(objs);
			else {
				this.caseMap.put(caseKey, objs);
			}
		}
		System.out.println("To obtain the form of all the elements:"
				+ this.caseMap);
	}

	public String getParamValue(String name) {
		List objs = (List) this.caseMap.get(name.toLowerCase());
		if (objs == null) {
			return null;
		}
		if (objs.size() == 0) {
			return null;
		}

		Object obj = objs.get(0);
		if (obj == null) {
			return null;
		}

		return obj.toString();
	}

	public Object[] getParamValues(String name) {
		List objs = (List) this.caseMap.get(name.toLowerCase());

		if (objs == null) {
			return new Object[0];
		}
		if (objs.size() == 0) {
			return new Object[0];
		}

		ArrayList list = new ArrayList();

		for (int i = 0; i < objs.size(); i++) {
			Object obj = objs.get(i);
			if (obj != null) {
				list.add(obj.toString());
			}
		}

		return list.toArray(new String[list.size()]);
	}

	public UploadFile getParamFile(String name) {
		List objs = (List) this.caseMap.get(name.toLowerCase());
		if (objs == null) {
			return null;
		}
		if (objs.size() == 0) {
			return null;
		}

		Object obj = objs.get(0);
		if (obj == null) {
			return null;
		}
		UploadFile uploadFile = new UploadFile();
		uploadFile.setFile((File) obj);
		uploadFile
				.setFileName(getParamValue((name + "FileName").toLowerCase()));
		uploadFile.setContentType(getParamValue((name + "ContentType")
				.toLowerCase()));
		return uploadFile;
	}

	public UploadFile[] getParamFiles(String name) {
		Object[] objs = ((ArrayList) this.caseMap.get(name.toLowerCase()))
				.toArray();
		Object[] fileNames = ((ArrayList) this.caseMap.get((name + "FileName")
				.toLowerCase())).toArray();
		Object[] contentTypes = ((ArrayList) this.caseMap
				.get((name + "ContentType").toLowerCase())).toArray();
		if (objs == null) {
			return new UploadFile[0];
		}
		if (objs.length == 0) {
			return new UploadFile[0];
		}

		ArrayList list = new ArrayList();
		for (int i = 0; i < objs.length; i++) {
			Object obj = objs[i];
			if ((obj == null) || (!(obj instanceof File)))
				continue;
			UploadFile uploadFile = new UploadFile();
			uploadFile.setFile((File) obj);
			uploadFile.setFileName(fileNames[i].toString());
			uploadFile.setContentType(contentTypes[i].toString());
			list.add(uploadFile);
		}

		return (UploadFile[]) list.toArray(new UploadFile[list.size()]);
	}

	public Map<String, Object> getParamMap(Object[] objects) {
		Map map = new HashMap();

		if (objects != null) {
			for (Object object : objects) {
				map.put(((String) object).toLowerCase(),
						getParamValue((String) object));
			}
		}
		System.out.println("generation of Map objects:" + map);

		return map;
	}

	public Map<String, Object> getParamMap(String[] strs, Object[] objects) {
		Map map = getParamMap(objects);
		if (strs != null) {
			for (String str : strs) {
				Object[] _objects = getParamValues(str);
				map.put(str.toLowerCase(), _objects);
			}
		}
		System.out.println("generation of Map objects:" + map);
		return map;
	}

	public Map<String, Object> getParamMap(String[] strs, String[] fileNames,
			Object[] objects) {
		Map map = getParamMap(strs, objects);
		if (fileNames != null) {
			for (String fileName : fileNames) {
				map.put(fileName.toLowerCase(), getParamFiles(fileName
						.toLowerCase()));
			}
		}
		System.out.println("generation of Map objects:" + map);
		return map;
	}

	public FormRead getParamObject(Object[] objects) {
		Object[] objects2 = new Object[objects.length];
		this._objects = objects2;

		if (objects != null) {
			for (int i = 0; i < objects.length; i++) {
				objects2[i] = getParamValue((String) objects[i]);
			}
		}
		return this;
	}

	public FormRead addObject(Object[] objects) {
		Object[] objects2 = new Object[objects.length];
		if (objects != null) {
			for (int i = 0; i < objects.length; i++) {
				objects2[i] = objects[i].toString();
			}
		}
		Object[] obj = new Object[this._objects.length + objects2.length];

		System.arraycopy(this._objects, 0, obj, 0, this._objects.length);
		System.arraycopy(objects2, 0, obj, this._objects.length,
				objects2.length);

		this._objects = obj;

		return this;
	}

	public Object[] object() {
		return this._objects;
	}
}